using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace MatrixConfiguration.HttpApi.Host.Extensions;

/// <summary>
/// 健康检查扩展方法
/// </summary>
public static class HealthCheckExtensions
{
    /// <summary>
    /// 添加Matrix框架标准健康检查
    /// </summary>
    public static IHealthChecksBuilder AddMatrixHealthChecks(this IServiceCollection services)
    {
        return services.AddHealthChecks()
            // 数据库健康检查
            .AddCheck<DatabaseHealthCheck>("database")

            // Redis健康检查
            .AddCheck<RedisHealthCheck>("redis")

            // RabbitMQ健康检查
            .AddCheck<RabbitMQHealthCheck>("rabbitmq")

            // 服务自身健康检查
            .AddCheck<MatrixConfiguration.HttpApi.Host.HealthChecks.ConfigurationHealthCheck>("configuration-service")

            // 内存使用情况检查
            .AddCheck<MemoryHealthCheck>("memory")

            // 磁盘空间检查
            .AddCheck<DiskSpaceHealthCheck>("disk");
    }

    // /// <summary>
    // /// 添加服务发现健康检查
    // /// </summary>
    // public static IServiceCollection AddServiceDiscoveryHealthChecks(this IServiceCollection services)
    // {
    //     return services.AddHealthChecks()
    //         .AddCheck<MatrixServiceDiscovery.HttpApi.Host.HealthChecks.ServiceDiscoveryHealthCheck>("service-discovery-service")
    //         .AddCheck<DatabaseHealthCheck>("database")
    //         .AddCheck<RedisHealthCheck>("redis")
    //         .AddCheck<MemoryHealthCheck>("memory");
    // }
    //
    // /// <summary>
    // /// 添加通知服务健康检查
    // /// </summary>
    // public static IServiceCollection AddNotificationHealthChecks(this IServiceCollection services)
    // {
    //     return services.AddHealthChecks()
    //         .AddCheck<Matrix.Notification.HttpApi.Host.HealthChecks.NotificationHealthCheck>("notification-service")
    //         .AddCheck<DatabaseHealthCheck>("database")
    //         .AddCheck<RedisHealthCheck>("redis")
    //         .AddCheck<RabbitMQHealthCheck>("rabbitmq")
    //         .AddCheck<MemoryHealthCheck>("memory");
    // }
    //
    // /// <summary>
    // /// 添加用户管理健康检查
    // /// </summary>
    // public static IServiceCollection AddUserManagementHealthChecks(this IServiceCollection services)
    // {
    //     return services.AddHealthChecks()
    //         .AddCheck<UserManagementService.HttpApi.Host.HealthChecks.UserManagementHealthCheck>("user-management-service")
    //         .AddCheck<DatabaseHealthCheck>("database")
    //         .AddCheck<RedisHealthCheck>("redis")
    //         .AddCheck<MemoryHealthCheck>("memory");
    // }
    //
    // /// <summary>
    // /// 添加租户管理健康检查
    // /// </summary>
    // public static IServiceCollection AddTenantManagementHealthChecks(this IServiceCollection services)
    // {
    //     return services.AddHealthChecks()
    //         .AddCheck<TenantManagementService.HttpApi.Host.HealthChecks.TenantManagementHealthCheck>("tenant-management-service")
    //         .AddCheck<DatabaseHealthCheck>("database")
    //         .AddCheck<RedisHealthCheck>("redis")
    //         .AddCheck<MemoryHealthCheck>("memory");
    // }
    //
    // /// <summary>
    // /// 添加认证授权健康检查
    // /// </summary>
    // public static IServiceCollection AddAuthServiceHealthChecks(this IServiceCollection services)
    // {
    //     return services.AddHealthChecks()
    //         .AddCheck<AuthService.HttpApi.Host.HealthChecks.AuthServiceHealthCheck>("auth-service")
    //         .AddCheck<DatabaseHealthCheck>("database")
    //         .AddCheck<RedisHealthCheck>("redis")
    //         .AddCheck<MemoryHealthCheck>("memory");
    // }
}

/// <summary>
/// 数据库健康检查
/// </summary>
public class DatabaseHealthCheck : IHealthCheck
{
    public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
    {
        // TODO: 实现实际的数据库连接检查
        return Task.FromResult(HealthCheckResult.Healthy("数据库连接正常"));
    }
}

/// <summary>
/// Redis健康检查
/// </summary>
public class RedisHealthCheck : IHealthCheck
{
    public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
    {
        // TODO: 实现实际的Redis连接检查
        return Task.FromResult(HealthCheckResult.Healthy("Redis连接正常"));
    }
}

/// <summary>
/// RabbitMQ健康检查
/// </summary>
public class RabbitMQHealthCheck : IHealthCheck
{
    public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
    {
        // TODO: 实现实际的RabbitMQ连接检查
        return Task.FromResult(HealthCheckResult.Healthy("RabbitMQ连接正常"));
    }
}

/// <summary>
/// 内存健康检查
/// </summary>
public class MemoryHealthCheck : IHealthCheck
{
    public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
    {
        // 简单的内存使用检查
        var memoryUsage = GC.GetTotalMemory(false) / 1024 / 1024; // MB

        if (memoryUsage > 1000) // 超过1GB
        {
            return Task.FromResult(HealthCheckResult.Degraded($"内存使用较高: {memoryUsage}MB"));
        }

        return Task.FromResult(HealthCheckResult.Healthy($"内存使用正常: {memoryUsage}MB"));
    }
}

/// <summary>
/// 磁盘空间健康检查
/// </summary>
public class DiskSpaceHealthCheck : IHealthCheck
{
    public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
    {
        // TODO: 实现实际的磁盘空间检查
        return Task.FromResult(HealthCheckResult.Healthy("磁盘空间充足"));
    }
}
